#ifndef HANDLEHPOINT_HPP
#define HANDLEHPOINT_HPP

#include <stdio.h>
#include <cassert>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include "json11/json11.hpp"
#include <list>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <type_traits>
#include <fstream>
#include "sys/io.h"
#include <unistd.h>

using namespace std;
using namespace json11;
void writePointJson(Json::array p_total,string file_path){
	string sspace = "			";
	ofstream inputJson(file_path.c_str(), ios::trunc);
	if(!inputJson.is_open()){
			cout << "failed to open point.json" << endl;
			return ;
	}
	int p_num = p_total.size();
	// string s_temp;
	
	// write json
	inputJson << "{" << endl;
	inputJson << "	" << "\"data\": [" << endl;
	
	for (int i = 0; i < p_num; i++) {
	
		inputJson << "		" << "{" << endl;
		inputJson << sspace << "\"angle\"" << ":  " << p_total[i]["angle"].number_value() << "," << endl;
		inputJson << sspace << "\"gridX\"" << ":  " << p_total[i]["gridX"].number_value() << ","  << endl;
		inputJson << sspace << "\"gridY\"" << ":  " << p_total[i]["gridY"].number_value() << "," << endl;
		inputJson << sspace << "\"mapName\"" << ":  "<< "\"" << p_total[i]["mapName"].string_value() << "\"" << "," << endl;
		inputJson << sspace << "\"name\"" << ":  " << "\"" << p_total[i]["name"].string_value() << "\"" << "," << endl;
		//inputJson << sspace << "\"name\"" << ":  " << "\"" << p_total[i]["name"].string_value() << "\"" << "," << endl;
		inputJson << sspace << "\"type\"" << ":  " << "\""<<  p_total[i]["type"].string_value() << "\""<< endl;
		inputJson << "		" << "}";
		if (i < (p_num - 1)) {
			inputJson << "," << endl;
		}
	}

	inputJson << "	" << "]" << endl;
	inputJson << "}";

	inputJson.close();
}
int pointWrite(Json p1, string file_path) {
	Json::array p_total;
	if (access(file_path.c_str(), 0) != 0) {
		//creat new point.json
		ofstream out_file(file_path.c_str());
		out_file.close();
	}
	else {
		
		ifstream in_file(file_path.c_str());
		if(!in_file.is_open()){
			cout << "failed to open point.json" << endl;
			return -1;
		}
		stringstream buffer;
		string err;
		buffer << in_file.rdbuf();
		//cout << "parse result: " << buffer.str() << endl;
		Json test_tmp = Json::parse(buffer.str(), err);
		p_total = test_tmp["data"].array_items();
		//cout << test_tmp.dump() << endl;
		in_file.close();
	}

	p_total.push_back(p1);

	writePointJson(p_total,file_path);
	cout<<"pointWrite successful!"<<endl;
	return 0;
}



int pointDelete(string deleteKey, string file_path) {
	Json::array p_total;
	if (access(file_path.c_str(), 0) != 0) {
		
		cout << "file do not exist!!" << endl;
		return -1;
	}
	else {

		ifstream in_file(file_path.c_str());
		if(!in_file.is_open()){
			cout << "failed to open point.json" << endl;
			return -1;
		}
		stringstream buffer;
		string err;
		buffer << in_file.rdbuf();
		//cout << "parse result: " << buffer.str() << endl;
		Json test_tmp = Json::parse(buffer.str(), err);
		p_total = test_tmp["data"].array_items();
		//cout << test_tmp.dump() << endl;
		in_file.close();
	}
	int p_num = p_total.size();
	// string s_temp;
	Json::object object_;
    Json::array point_info;
	for(int i = 0; i<p_num;i++){
		object_ = p_total[i].object_items();
		if (p_total[i]["name"].string_value() != deleteKey) {
			Json map_list_(object_);
			point_info.push_back(map_list_);
		}
	}
	writePointJson(point_info,file_path);
	cout<<"pointDelete successful!"<<endl;
	return 0;

}

int pointMapEdit(string MapKey, string file_path) {
	Json::array p_total;
	if (access(file_path.c_str(), 0) != 0) {
		
		cout << "file do not exist!!" << endl;
		return -1;
	}
	else {

		ifstream in_file(file_path.c_str());
		if(!in_file.is_open()){
			cout << "failed to open point.json" << endl;
			return -1;
		}
		stringstream buffer;
		string err;
		buffer << in_file.rdbuf();
		//cout << "parse result: " << buffer.str() << endl;
		Json test_tmp = Json::parse(buffer.str(), err);
		p_total = test_tmp["data"].array_items();
		//cout << test_tmp.dump() << endl;
		in_file.close();
	}
	int p_num = p_total.size();

	Json::object object_;
    Json::array point_info;
	for(int i = 0; i<p_num;i++){
		object_ = p_total[i].object_items();
		object_["mapName"] = MapKey;
		
		Json map_list_(object_);
		point_info.push_back(map_list_);
	}

	writePointJson(point_info,file_path);
	cout<<"pointMapEdit successful!"<<endl;
	return 0;

}
#endif